<template>
  <a-table
    :dataSource="dataSource"
    :columns="columnsFilter"
    :pagination="query"
    @change="onChange"
    :size="lineheight"
    :row-key="record => record.id"
  >
    <template #bodyCell="{ column = {}, text, record }">
      <template v-if="column.key === 'createTime'">
        <span>
          {{
            record.createTime
              ? dayjs.utc(record.createTime).format('YYYY-MM-DD HH:mm:ss')
              : ''
          }}
        </span>
      </template>
      <template v-if="column.key === 'operation'">
        <detail-button :data="record" />
      </template>
    </template>
    <template #emptyText>
      <div
        style="
          display: flex;
          justify-content: center;
          align-items: center;
          height: 100%;
        "
      >
        <a-spin :spinning="loading">
          <a-empty
            :image="notData"
            :description="loading ? '加载中...' : '暂无数据'"
          />
        </a-spin>
      </div>
    </template>
  </a-table>
</template>
<script setup>
import { onMounted, computed, watch } from 'vue'
import ATable from '@/components/table.vue'
import dayjs from 'dayjs'
import notData from '@/assets/images/not-data.png'
import detailButton from './table-content-detail.vue'
import { useRoute } from 'vue-router'
import {
  useColumns,
  useQuery,
  useDataSourceManager,
  useDataSource,
  useLineheight,
  useLoading,
  useFiltersLog,
} from './provide-context.js'

/**
 * 全局数据
 */
let route = useRoute()
let columns = useColumns()
let dataSource = useDataSource()
let query = useQuery()
let loading = useLoading()
let lineheight = useLineheight()
let filtersLog = useFiltersLog()
let { update } = useDataSourceManager()
/**
 * 表头
 */
let columnsFilter = computed(() => {
  // 给表格列添加过滤和排序
  columns.value.forEach(e => {
    if (e.key === 'operationModule') {
      e.filteredValue = filtersLog?.value?.operationModule || null
    }
  })
  return columns?.value?.filter(({ checked }) => checked) || []
})
/**
 * table组件分页、排序、筛选变化事件
 */
function onChange({ current, pageSize }, filters, sorter, { action }) {
  query.value.current = current
  query.value.pageSize = pageSize
  if (action !== 'paginate') {
    query.value.current = 1
    query.value.pageSize = 10
  }
  filtersLog.value = filters
  update()
}
//初始化调用
//初始化调用
onMounted(() => {
  query.value.current = 1
  query.value.pageSize = 10
  query.value.dataId = route?.params?.id
  update()
})
</script>
